package help

import (
	"gitee.com/ameise84/e3utils/cache"
	"gitee.com/ameise84/e3utils/str_conv"
	"hash/crc64"
	"math"
)

var _gHashMap64 cache.Cache[string]
var _gHashMap32 cache.Cache[string]

func init() {
	_gHashMap64 = cache.New[string](cache.DefaultOptions().SetSize(4096).SetTTL(0))
	_gHashMap32 = cache.New[string](cache.DefaultOptions().SetSize(4096).SetTTL(0))
}

func Hash64(s string) uint64 {
	if len(s) == 0 {
		return 0
	}
	if v, ok := _gHashMap64.Load(s); ok {
		return v.(uint64)
	}

	h := crc64.MakeTable(crc64.ECMA)
	n := crc64.Checksum(str_conv.ToBytes(s), h)
	_gHashMap64.Store(s, n)
	//logger.TracePrintf("%s -> Hash64 -> %d)", s, n)
	return n
}

func Hash32(s string) uint64 {
	return Hash64(s) % (math.MaxUint32 + 1)
}

func Hash16(s string) uint64 {
	return Hash64(s) % (math.MaxUint16 + 1)
}
